package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.DepartmentManagement;
import com.example.prison.entity.PeopleManage;
import com.example.prison.service.DepartmentManagementService;
import com.example.prison.service.PeopleManageService;
import com.example.prison.service.SpecialInfoService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "员工管理")
@RestController
@RequestMapping("/api")
public class PeopleManageController {

    @Autowired
    private PeopleManageService peopleManageService;

    @Autowired
    private SpecialInfoService specialInfoService;

    @Autowired
    private DepartmentManagementService departmentManagementService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取人员列表")
    @GetMapping("/getPeople")
    public Response<PageInfo<PeopleManage>> getPeople(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                      @RequestParam(required = false)@Parameter(description = "姓名") String name,
                                                      @RequestParam(required = false)@Parameter(description = "状态（营区内/营区外）") String status,
                                                      @RequestParam(required = false)@Parameter(description = "部门") String unit,
                                                      @RequestParam(required = false)@Parameter(description = "职位") String post,
                                                      @RequestParam(required = false)@Parameter(description = "联系方式") String phone,
                                                      @RequestParam(required = false)@Parameter(description = "身份证号") String idCard) {
        PageInfo<PeopleManage> pageInfo = peopleManageService.get(pageNumber, pageSize, name, unit, post, status, phone, idCard);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增人员")
    @PostMapping("/addPeople")
    public Response<?> addPeople(@RequestBody PeopleManage data) {
        int result = peopleManageService.add(data);
        if (result == -1) {
            return Response.error("该人员已存在！");
        }
        log.info("新增人员"+", 数据："+ JSON.toJSONString(data));
        systemLogService.add("新增人员");
        return Response.success("新增人员成功！");
    }

    @ApiOperation("依据id删除人员数据")
    @PostMapping("/deletePeople")
    public Response<?> deletePeople(@Parameter(description = "ID") @RequestBody Integer[] ids) {
        peopleManageService.delete(ids);
        log.info("删除人员数据");
        systemLogService.add("删除人员数据");
        return Response.success("删除成功！");

    }

    @ApiOperation("编辑人员")
    @PostMapping("/updatePeople")
    public Response<?> updatePeople(@RequestBody PeopleManage data) {
        Integer id = data.getId();
        int result = peopleManageService.update(data, id);
        if (result == 0) {
            return Response.error("该人员已存在！");
        }
        log.info("编辑人员"+", 数据："+JSON.toJSONString(data));
        systemLogService.add("编辑人员");
        return Response.success("更新成功！");
    }

    @ApiOperation("依据id获取人员信息")
    @GetMapping("/getPeopleById")
    public Response<PeopleManage> getPeopleById(@Parameter(description = "ID")@RequestParam Integer id) {
        PeopleManage data = peopleManageService.getById(id);
        if(ObjectUtils.isEmpty(data)) {
            return Response.error("此人员id不存在！");
        }
        return Response.success("获取成功！", data);
    }

    @ApiOperation("依据职位获取人员列表")
    @GetMapping("/getPeopleByPost")
    public Response<PageInfo<PeopleManage>> getPeopleByPost(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                  @Parameter(description = "职位")@RequestParam String post) {
        PageHelper.startPage(pageNumber, pageSize);
        List<PeopleManage> data = peopleManageService.getByPost(post);
        PageInfo<PeopleManage> pageInfo = new PageInfo<>(data);
        if(ObjectUtils.isEmpty(data)) {
            return Response.error("此人员id不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("获取全部部门的人员列表")
    @GetMapping("/getPeopleOrderDepart")
    public Response<?> getPeopleOrderDepart() {
        Map<String, List<PeopleManage>> map = new HashMap<>();
        List<DepartmentManagement> departs = departmentManagementService.list();
        for (DepartmentManagement depart : departs) {
            List<PeopleManage> data = peopleManageService.getByUnit(depart.getDepartName());
            map.put(depart.getDepartName(),data);
        }
        return Response.success("获取成功！", map);
    }


}
